package Control;

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.*;
import java.util.Date;
import Database.*;
import Model.*;

import javax.imageio.ImageIO;
import javax.swing.*;

public class ProductGUI extends JFrame implements ActionListener
{
        private JRadioButton drink, alcoh, food, clean;
        private Product pro;
        private JLabel logoPicture;
        private JPanel topPanel, bottomPanel, logo;
        private JButton ok, cancel;
        private TableOperations to;
       
        public ProductGUI(Product p)
        {
                pro = p;
                
                setSize(245, 380);
                setLocationRelativeTo(null);
                setResizable(false);
                to = new TableOperations();
       
        setLayout(new GridLayout(3,1));
       
        GridBagLayout gridbagLayoutSettings = new GridBagLayout();
        GridBagConstraints gc = new GridBagConstraints();  
        Insets insets = new Insets(5,5,5,5);
       
        BufferedImage myPicture = null;
        try
        {
                myPicture = ImageIO.read(new File("logo.png"));
        }
        catch (IOException e)
        {
                e.printStackTrace();
        }
        logo = new JPanel();
        logoPicture = new JLabel(new ImageIcon(myPicture));
        logo.add(logoPicture);
        this.add(logo);
       
        //Panel 1
        topPanel = new JPanel();
        topPanel.setLayout(gridbagLayoutSettings);
       
        drink = new JRadioButton("Drink");
        gc.gridx = 0;    
        gc.gridy = 1;    
        gc.weighty = 0;    
        gc.weightx = 0;      
        gc.insets = insets;
        gc.fill = GridBagConstraints.BOTH;
        topPanel.add(drink, gc);
       
        alcoh = new JRadioButton("Alcoholic");
        gc.gridx = 0;    
        gc.gridy = 3;    
        gc.weighty = 0;    
        gc.weightx = 0;      
        gc.insets = insets;
        gc.fill = GridBagConstraints.BOTH;
        topPanel.add(alcoh, gc);
       
        food = new JRadioButton("Food");
        gc.gridx = 0;    
        gc.gridy = 5;    
        gc.weighty = 0;    
        gc.weightx = 0;      
        gc.insets = insets;
        gc.fill = GridBagConstraints.BOTH;
        topPanel.add(food, gc);
       
        clean = new JRadioButton("Cleaning Product");
        gc.gridx = 0;    
        gc.gridy = 7;    
        gc.weighty = 0;    
        gc.weightx = 0;      
        gc.insets = insets;
        gc.fill = GridBagConstraints.BOTH;
        topPanel.add(clean, gc);
       
        this.add(topPanel, BorderLayout.CENTER);
       
        //Panel 2
        bottomPanel = new JPanel();
        bottomPanel.setLayout(gridbagLayoutSettings);

        ok = new JButton("OK");
        gc.gridx = 1;    
        gc.gridy = 1;    
        gc.weighty = 0;    
        gc.weightx = 0;      
        gc.insets = insets;
        gc.fill = GridBagConstraints.BOTH;
        ok.addActionListener(this);
        bottomPanel.add(ok, gc);

        cancel = new JButton("Cancel");
        gc.gridx = 3;    
        gc.gridy = 1;    
        gc.weighty = 0;    
        gc.weightx = 0;      
        gc.insets = insets;
        gc.fill = GridBagConstraints.BOTH;
        cancel.addActionListener(this);
        bottomPanel.add(cancel, gc);
       
        this.add(bottomPanel);
       
        }
       
        public void actionPerformed(ActionEvent e)
        {
                int type = 0;
                if(e.getSource() == ok)
                {
                        if(drink.isSelected())
                                type = 1;
                        else if(alcoh.isSelected())
                                type = 2;
                        else if(clean.isSelected())
                                type = 3;
                        else if(food.isSelected())
                                type = 4;
                       
                        if(type == 0)
                        {
                                JOptionPane.showMessageDialog(null, "Please select a prodct type");
                        }
                        else
                        {
                                to.addProduct(pro, type);
                                JOptionPane.showMessageDialog(null, "Product added");
                                this.dispose();
                        }
                                         
                }                
        }
}
